package ifi.p17.thai_cisse;

import javax.swing.table.AbstractTableModel;
import java.util.ArrayList;
import java.util.ResourceBundle;

/**
 * Cett class definition une model pour control donnes de Jtable
 */
class CarnetTableModel extends AbstractTableModel {

    private static ResourceBundle resAddressBook = ResourceBundle.getBundle("carnet_adresse");
    public static final int COL_NOM = 0;
    public static final int COL_NUMERO_DE_TELEPHONE = 1;
    public static final int COL_ADRESSE_DE_TRAVAIL = 2;
    public static final int COL_ADRESSE_DE_DOMICILE = 3;

    public static final String TEXT_NOM = resAddressBook.getString("Nom");
    public static final String TEXT_NUMERO_DE_TELEPHONE = resAddressBook.getString("Numero_de_telephone");
    public static final String TEXT_ADRESSE_DE_TRAVAIL = resAddressBook.getString("Adresse_de_travail");
    public static final String TEXT_ADRESSE_DE_DOMICILE = resAddressBook.getString("Adress_de_domicile");


    private String[] columnNames = { TEXT_NOM, TEXT_NUMERO_DE_TELEPHONE,
            TEXT_ADRESSE_DE_TRAVAIL, TEXT_ADRESSE_DE_DOMICILE };

    private ArrayList<Personne> data;

    CarnetTableModel() {
        this.data = new ArrayList<Personne>();
    }

    CarnetTableModel(ArrayList<Personne> pData ){
        this.data = pData;
    }
    public ArrayList<Personne> getData(){
        return data;
    }

    public void setData(ArrayList<Personne> data) {
        this.data = data;
    }

    public void resetData(){
        this.data.clear();
        fireTableDataChanged();
    }

    public void addCarte(Personne personne){
        try {
            data.add(personne);
            fireTableDataChanged();
        } catch ( NullPointerException e) {};
    }

    public void editCarte(Personne personne, int selectedRow){
        data.set(selectedRow, personne);
        fireTableDataChanged();
    }

    public void deleteCarte( int selectedRow){
        data.remove(selectedRow);
        fireTableDataChanged();
    }

    public ArrayList<Personne> searchByName(String personName) {

        return searchByName( personName, this.data );
    }

    public ArrayList<Personne> searchByName(String personName, ArrayList<Personne> aData) {
        ArrayList<Personne> resutListPerson = new ArrayList<Personne>();
        for (int i = 0; i < aData.size(); i++) {
            if (aData.get(i).getNom().toLowerCase().indexOf(personName.toLowerCase()) != -1) {
                resutListPerson.add(aData.get(i));
            }
        }
        return resutListPerson;
    }

    public int getColumnCount() {
        return columnNames.length;
    }

    public int getRowCount() {
        int len = 0;
        try {
            len = data.size();
        } catch ( NullPointerException e) {};
        return len;
    }

    public String getColumnName(int col) {
        return columnNames[col];
    }

    public Object getValueAt(int row, int col) {
        String value = "";
        try {
            switch (col) {
                case COL_NOM:
                    value = data.get(row).getNom();
                    break;
                case COL_NUMERO_DE_TELEPHONE:
                    value = data.get(row).getNumeroDeTelephone();
                    break;
                case COL_ADRESSE_DE_TRAVAIL:
                    value = data.get(row).getAdresseDeTraivail();
                    break;
                case COL_ADRESSE_DE_DOMICILE:
                    value = data.get(row).getAdresseDeDomicile();
                    break;
            }
        } catch (ArrayIndexOutOfBoundsException ae) {};
        return value;
    }
}